using B.S.Databasic.Api.Read.Application.Command.Permission;
using B.S.Databasic.Domain.RBAC;
using B.S.Databasic.ErrorCode;
using B.S.Databasic.Infrastructure.Interface;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace B.S.Databasic.Api.Read.Application.Handler.Permission
{
    public class GetAllPermissionHandler : IRequestHandler<GetAllPermissionCommand, ApiResult<List<PermissionModel>>>
    {
        private readonly IBaseRepository<PermissionModel> _permissionRepo;
        public GetAllPermissionHandler(IBaseRepository<PermissionModel> permissionRepo)
        {
            _permissionRepo = permissionRepo;
        }
        public async Task<ApiResult<List<PermissionModel>>> Handle(GetAllPermissionCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<List<PermissionModel>>();
            try
            {
                var list = _permissionRepo.GetAll().Where(x => !x.IsDeleted).ToList();
                res.Data = list;
                res.Msg = "查询所有权限成功!";
                res.Code = ApiEnum.Success;
            }
            catch (Exception ex)
            {
                res.Data = null;
                res.Msg = "查询所有权限异常：" + ex.Message;
                res.Code = ApiEnum.Error;
            }
            return await Task.FromResult(res);
        }
    }
}